<?php
include_once ('Include/init.php');

//处理浏览器传来的url
$urls = isset($_GET['urls']) ? trim($_GET['urls']) : 'Index/Show';

//unset()  $_GET['urls']
if (isset($_GET['urls'])) unset($_GET['urls']);

//处理get传过来的数值
$params = explode('/', $urls);
$count = count($params);
//如果输入的url错误，将被跳转到其他页面
if ($count>0 && $count<3){
	if (ucfirst($params[0]) != 'Admin' && ucfirst($params[0] != 'Page')){
		array_unshift($params, 'Page');
	}
}

$appName = ucfirst(isset($params[0]) ? $params[0] : 'Page');
$ctrName = ucfirst(isset($params[1]) ? $params[1] : 'Index');
$mtdName = isset($params[2]) ? $params[2] : 'Show';

//当$count大于2时，action为第三个，然后是key加上它的value
if ($count > 2){
	for ($i=3; $i<$count; $i=$i+2){
		$_GET[$params[$i]] = isset($_GET[$params[$i+1]]) ? $_GET[$params[$i+1]] : '';
	}
}

define('APP_NAME', $appName);

//调用接口
$filename = ROOT_DIR . "Controllers/{$appName}/{$ctrName}.php";

if (!file_exists($filename)){
	exit('Page Not Found');
}
require_once $filename;

$ctrIst = new $ctrName;
$ctrIst->mtdName();
//自动加载文件
function __autoload($classname)
{
	$findPath = array(
		ROOT_DIR . 'Libraries/' . $classname . '.php',
		ROOT_DIR . 'Controller/' . APP_NAME . '/' . $classname . '.php',
		ROOT_DIR . 'Modules/' . $classname . '.php',
	);
	
	foreach ($findPath as $filename){
		if (file_exists($filename)){
			require_once $filename;
			return true;
	}	
		return false;
	}
}